package com.gossip;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;
/**
 * The object represents a gossip member with the properties as received from a remote gossip member. 
 */
public class RemoteGossipMember extends GossipMember {
	public RemoteGossipMember(String name, String host, int port, Date heartbeat, int memberId) {
		super(host, port, heartbeat, name);
		this._memberId = memberId;
	}
	
	public RemoteGossipMember(String name, String host, int port, Date heartbeat, int memberId, int failedCount) {
		super(host, port, heartbeat, name);
		this._memberId = memberId;
		this._failedCount = failedCount;
	}
	
	protected int _failedCount = 0;
	
	public void setFailedCount(int count)
	{
		this._failedCount = count;
		super.setFailedCount(count);
	}
	
	public int getFailedCount()
	{
		return this._failedCount;
	}
	
	public JSONObject toJSONObject() {
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("memberId", _memberId);
			jsonObject.put("name", super._name);
			jsonObject.put("host", _host);
			jsonObject.put("port", _port);
			jsonObject.put("heartbeat", _heartbeat.getTime());
			jsonObject.put("failedCount", _failedCount);
			
			return jsonObject;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public JSONObject toJSONObject(boolean withoutFailedCount) {
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("memberId", _memberId);
			jsonObject.put("host", _host);
			jsonObject.put("port", _port);
			jsonObject.put("heartbeat", _heartbeat.getTime());
			if(!withoutFailedCount) jsonObject.put("failedCount", _failedCount);
			return jsonObject;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String toString() {
		return "Member [r, memberId = "+_memberId+", address=" + getAddress() + ", heartbeat=" + _heartbeat.toString()+", failedCount="+_failedCount + "]";
	}
}
